package com.example.truthordare;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;

import android.widget.Toast;

public class MainActivity extends Activity {
	/**
	 * 
	 */

	DBAdapter db;
	public ListView listPlayer;
	Context mcontext;
	ArrayList<Player> players = new ArrayList<Player>();
	static ArrayList<Tempplaygame> tempplayer = new ArrayList<Tempplaygame>();

	Cursor c;
	int lv = 1;
	private static final String TAG = MainActivity.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mcontext = this;
		db = new DBAdapter(mcontext);

		listPlayer = (ListView) findViewById(R.id.listView1);
		ImageButton imadd = (ImageButton) findViewById(R.id.img_add);
		ImageButton imadd1 = (ImageButton) findViewById(R.id.img_add1);
		ImageButton implay = (ImageButton) findViewById(R.id.img_play);
		ImageButton imasitting = (ImageButton) findViewById(R.id.img_setting);
		// listPlayer.setItemsCanFocus(false);
		
		LoadGrid();
		DataTranfer.getInstance().setList(tempplayer);

		NameAdapter nameadapter = new NameAdapter(getApplicationContext(),
				players);
		listPlayer.setAdapter(nameadapter);
		// add player
		imadd.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(), Addplayer.class);
				startActivity(i);
				finish();
			}
		});

		imadd1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getApplicationContext(), Addquestion.class);
				startActivityForResult(i, 1);
				
				
				
			}
		});

		imasitting.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(), Setting.class);
				startActivityForResult(i, 0);
				// finish();

			}
		});

		implay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				checkquestion();
				final int send = lv;
				Log.i("lv", "" + lv);
				Bundle sendBundle = new Bundle();
				sendBundle.putInt("lv", send);
				// Log.i("click play", "1");
				// sendBundle.putStringArray("list", players);
				// sendBundle.putSerializable("list", players);
				Intent i = new Intent(getApplicationContext(), Playgame.class);
				i.putExtras(sendBundle);
				startActivityForResult(i, 1);
			}
		});

	}

	public void checkquestion() {
		c = db.chekquestion();
		int i = c.getCount();
		if (i < 1) {
			db.insertquestion();
			Toast.makeText(getApplicationContext(),
					"Insert question have done", Toast.LENGTH_SHORT).show();
		}
		c.close();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (resultCode == RESULT_OK) {
			Bundle receivebd = data.getExtras();
			if (receivebd != null) {
				lv = receivebd.getInt("lv", 1);
			}
		}
	}

	public void LoadGrid() {
		try {
			players.clear();
			tempplayer.clear();
			Cursor c = db.getAllPlayer();
			while (c.moveToNext()) {
				String namePlayer = c.getString(c.getColumnIndex("NamePlayer"));
				String id = c.getString(c.getColumnIndex("_id"));
				int gender = c.getInt(c.getColumnIndex("Gender"));
				String contact = null;
				int ids = 0;
				Player player = new Player(id, namePlayer, gender, contact);
				int sroce = 0;
				Tempplaygame templayer = new Tempplaygame(namePlayer, ids,
						sroce);
				tempplayer.add(templayer);
				ids++;
				players.add(player);
				Log.i("load", "load ok");
			}
			c.close();
			
			
		} catch (Exception ex) {

			ex.printStackTrace();
		}finally{
//			c.close();
		}
	}
}